/*  Copyright (C) 2012 Philip Smith
 *
 *  This file is part of SuperChat.
 *
 *  SuperChat is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  SuperChat is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with SuperChat.  If not, see <http://www.gnu.org/licenses/>.
 */

package net.supertycoon.bukkit.superchat;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class cCommandHandler implements CommandExecutor {

	@Override
	public boolean onCommand(CommandSender arg0, Command arg1, String arg2, String[] arg3) {
		if (!(arg0 instanceof Player)) {
			arg0.sendMessage(ChatColor.RED.toString() + "Only a Player has access to this command");
			return true;
		}
		if (arg3.length == 0) {
			if (SuperChat.players.containsKey(arg0))
				SuperChat.players.get(arg0).refresh();
			return true;
		}
		if (!SuperChat.players.containsKey(arg0))
			SuperChat.players.put(arg0, new PlayerShit(arg0));
		ChatRoom room = SuperChat.players.get(arg0).last;
		if (room == null) {
			arg0.sendMessage(ChatColor.RED.toString() + "You do not have a recent chat room");
			return true;
		}
		if (!SuperChat.rooms.containsKey(room.name)) {
			SuperChat.players.get(arg0).last = null;
			arg0.sendMessage(ChatColor.RED.toString() + "You do not have a recent chat room (it no longer exists)");
			return true;
		}
		((Player) arg0).chat("/chat " + room.name + ' ' + SuperChat.concat(arg3, " "));
		return true;
	}

}
